package edu.sdwu.liqing.qasystem.aspect;

import edu.sdwu.liqing.qasystem.entity.QaUser;
import edu.sdwu.liqing.qasystem.model.Response;
import edu.sdwu.liqing.qasystem.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.SuppressAjWarnings;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Objects;

@Slf4j
@Aspect
@Order(1)
@Component
@SuppressAjWarnings
public class LoginAspect {


    @Autowired
    RequestUtil requestUtil;
    //声明一个切点
    //标注了Login注解的方法
    //顺便检验一下登录时长
    @Around("@annotation(edu.sdwu.liqing.qasystem.annotation.Login)")
    public Object doCheckLogin(ProceedingJoinPoint point) throws Throwable {
        Date now = new Date();
        MethodSignature method = (MethodSignature) point.getSignature();
        log.info("访问方法: {}", method.getMethod().getName());
        //开始校验是否登录
        QaUser user = requestUtil.getCurrentUser();
        //获取session中存储的username
        //校验属性
        if (Objects.isNull(user)) {
            log.info("未登录");
            return Response.create(401,"未登录", null);
        }
        return point.proceed();
    }

}
